package com.hyt.it.ogt.controller.ykcj.grade;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.annotation.Log;
import com.hyt.it.ogt.ykcj.common.core.domain.AjaxResult;
import com.hyt.it.ogt.ykcj.common.core.domain.entity.SysUser;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.enums.BusinessType;
import com.hyt.it.ogt.ykcj.common.enums.FileType;
import com.hyt.it.ogt.ykcj.common.utils.ServletUtils;
import com.hyt.it.ogt.ykcj.common.utils.StringUtils;
import com.hyt.it.ogt.ykcj.domain.grade.ExamineeSubjectGradeInfo;
import com.hyt.it.ogt.ykcj.domain.vo.AddExamineeGradeInfoVo;
import com.hyt.it.ogt.ykcj.domain.vo.ArchiveVo;
import com.hyt.it.ogt.ykcj.domain.vo.BatchArchiveGradeVo;
import com.hyt.it.ogt.ykcj.domain.vo.EditExamineeGradeInfoVo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamindeGradeInfoRsp;
import com.hyt.it.ogt.ykcj.domain.vo.ExamineeSubjectGradeInfoVo;
import com.hyt.it.ogt.ykcj.domain.vo.ImportGradeInfoVo;
import com.hyt.it.ogt.ykcj.domain.vo.QueryExamineeGradeVo;
import com.hyt.it.ogt.ykcj.framework.web.service.TokenService;
import com.hyt.it.ogt.ykcj.service.grade.IExamineeGradeInfoService;
import com.hyt.it.ogt.ykcj.service.grade.IExamineeSubjectGradeInfoService;
import com.hyt.it.ogt.ykcj.service.grade.impl.ImportDbfGradeServiceImpl;
import com.hyt.it.ogt.ykcj.service.grade.impl.ImportExcelGradeServiceImpl;

import cn.hutool.core.util.IdcardUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;

/**
 * 考生成绩信息Controller
 * 
 * @author admin
 * @date 2021-06-16
 */
@Api(tags = "grade.1.考生成绩信息", value = "考生成绩信息请求接口")
@ApiSort(value = 201)
@RestController
@RequestMapping("/grade/info")
public class ExamineeGradeInfoController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IExamineeGradeInfoService examineeGradeInfoService;// 考生成绩信息表

    @Autowired
    private IExamineeSubjectGradeInfoService examineeSubjectGradeInfoService;// 考生科目成绩信息

    @Autowired
    private ImportDbfGradeServiceImpl importDbfGradeService;

    @Autowired
    private ImportExcelGradeServiceImpl importExcelGradeService;



    /**
     * 查询考生成绩信息列表
     */
    @ApiOperation("查询考生成绩信息列表")
    @GetMapping("/queryList")
    public TableDataInfo list(QueryExamineeGradeVo queryExamineeGradeVo)
    {
        startPage();
        List<ExamindeGradeInfoRsp> list = examineeGradeInfoService.selectGradeInfoPageList(queryExamineeGradeVo);
        return getDataTable(list);
    }

    /**
     * 查询考生归档成绩信息列表
     */
    @ApiOperation("查询考生归档成绩信息列表")
    //@PreAuthorize("@ss.hasPermi('grade:info:queryArchivedList')")
    @GetMapping("/queryArchivedList")
    public TableDataInfo queryArchivedList(QueryExamineeGradeVo queryExamineeGradeVo)
    {
        startPage();
        List<ExamindeGradeInfoRsp> list = examineeGradeInfoService.queryArchivedList(queryExamineeGradeVo);
        return getDataTable(list);
    }

    /**
     * 获取考生成绩信息详细信息
     */
    @ApiOperation("获取考生成绩信息详细信息")
    @GetMapping(value = "/{gradeId}")
    public AjaxResult getInfo(@PathVariable("gradeId") String gradeId)
    {
        return AjaxResult.success(examineeGradeInfoService.selectExamineeGradeInfoById(gradeId));
    }

    /**
     * 新增考生成绩信息
     */
    @ApiOperation("新增考生成绩信息")
    @Log(title = "新增考生成绩信息", businessType = BusinessType.INSERT)
    @PostMapping("/addExamineeGradeInfo")
    public AjaxResult addExamineeGradeInfo(@Validated @RequestBody AddExamineeGradeInfoVo examineeGradeInfoVo)
    {
        if (!IdcardUtil.isValidCard(examineeGradeInfoVo.getExamineeIdCode())){
            return AjaxResult.error("证件号码格式错误");
        }
        SysUser user = tokenService.getSysUser(ServletUtils.getRequest());
        return toAjax(examineeGradeInfoService.insertExamineeGradeInfo(examineeGradeInfoVo,user));
    }

    /**
     * 修改考生信息
     */
    @ApiOperation("修改考生信息")
    @Log(title = "修改考生信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editExamineeGradeInfo")
    public AjaxResult editExamineeGradeInfo(@Validated @RequestBody EditExamineeGradeInfoVo examineeGradeInfo)
    {
        examineeGradeInfoService.updateExamineeGradeInfo(examineeGradeInfo);
        return AjaxResult.success();
    }


    /**
     * 删除考生成绩信息
     */
    @ApiOperation("删除考生成绩信息")
    @Log(title = "删除考生成绩信息", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteExamineeGradeInfo/{gradeId}")
    public AjaxResult deleteExamineeGradeInfo(@PathVariable String gradeId)
    {
        examineeGradeInfoService.deleteExamineeGradeInfoById(gradeId);
        return AjaxResult.success();
    }

    /**
     * 新增科目成绩
     */
    @ApiOperation("新增科目成绩")
    @Log(title = "新增科目成绩", businessType = BusinessType.UPDATE)
    @PostMapping("/addSubjectGrade")
    public AjaxResult addSubjectGrade(@Validated @RequestBody ExamineeSubjectGradeInfo subjectGradeInfo)
    {
        SysUser user = tokenService.getSysUser(ServletUtils.getRequest());
        examineeSubjectGradeInfoService.addExamineeSubjectGradeInfo(subjectGradeInfo,user);
        return AjaxResult.success();
    }

    /**
     * 修改科目成绩
     */
    @ApiOperation("修改科目成绩")
    @Log(title = "修改科目成绩", businessType = BusinessType.UPDATE)
    @PutMapping("/editSubjectGrade")
    public AjaxResult editSubjectGrade(@Validated @RequestBody ExamineeSubjectGradeInfoVo subjectGradeInfo)
    {
        examineeSubjectGradeInfoService.updateExamineeSubjectGradeInfo(subjectGradeInfo);
        return AjaxResult.success();
    }


    /**
     * 删除考生成绩信息
     */
    @ApiOperation("删除考生成绩信息")
    @Log(title = "删除考生科目成绩", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteSubjectGrade/{subjectGradeId}")
    public AjaxResult deleteSubjectGrade(@PathVariable String subjectGradeId)
    {
        examineeSubjectGradeInfoService.deleteExamineeSubjectGradeInfoById(subjectGradeId);
        return AjaxResult.success();
    }

    /**
     * 成绩归档
     */
    @ApiOperation("成绩归档")
    @Log(title = "成绩归档", businessType = BusinessType.UPDATE)
    @PostMapping("/archiveGrade")
    public AjaxResult archiveGrade(@RequestBody ArchiveVo archiveVo)
    {
        SysUser sysUser = tokenService.getSysUser(ServletUtils.getRequest());
        examineeGradeInfoService.archiveGradeInfo(archiveVo.getType(),archiveVo.getId(),sysUser);
        return AjaxResult.success("归档成功");
    }

    @ApiOperation("导入成绩")
    @Log(title = "导入成绩", businessType = BusinessType.IMPORT)
    @PostMapping("/importGradeInfo")
    public AjaxResult importGradeInfo(MultipartFile file, String examId, String examNameConfigId, String examYear) throws Exception
    {
        // 校验文件格式
        if(null == file){
            return AjaxResult.error("文件为空！");
        }
        String fileName = file.getOriginalFilename();
        if(StringUtils.isEmpty(fileName)){
            return AjaxResult.error("文件名称为空！");
        }
        String fileType = fileName.substring(fileName.indexOf("."));
        List<ImportGradeInfoVo> errorList = new ArrayList<>();
        if (FileType.FILE_TYPE_DBF.getType().equals(fileType)){
            errorList= importDbfGradeService.importFile(file,examId,examNameConfigId,examYear);
        }else if (FileType.FILE_TYPE_XLS.getType().equals(fileType) || FileType.FILE_TYPE_XLSX.getType().equals(fileType)){
            errorList =importExcelGradeService.importFile(file,examId,examNameConfigId,examYear);
        }else {
            return AjaxResult.error("文件类型错误！");
        }
        return AjaxResult.success(errorList);
    }

    @ApiOperation("导出成绩")
    @Log(title = "导出成绩", businessType = BusinessType.EXPORT)
    @GetMapping("/exportGradeInfo")
    public AjaxResult exportGradeInfo(BatchArchiveGradeVo queryExamineeGradeVo)
    {
        // 校验文件格式
        return examineeGradeInfoService.exportGradeInfo(queryExamineeGradeVo);
    }

    @ApiOperation("下载导入成绩模板")
    @Log(title = "下载导入成绩模板", businessType = BusinessType.DOWNLOAD)
    @GetMapping("/downloadTemplate")
    public AjaxResult downloadTemplate(QueryExamineeGradeVo queryExamineeGradeVo)
    {
        return examineeGradeInfoService.downloadTemplate(queryExamineeGradeVo);
    }

    /**
     * 批量成绩归档
     */
    @ApiOperation("批量成绩归档")
    @Log(title = "批量成绩归档", businessType = BusinessType.UPDATE)
    @PostMapping("/batchArchiveGrade")
    public AjaxResult batchArchiveGrade(@RequestBody BatchArchiveGradeVo batchArchiveGradeVo)
    {

        if (StringUtils.isEmpty(batchArchiveGradeVo.getExamId())){
            return AjaxResult.error("考试id不能为空");
        }
        // TODO 添加一张任务表，执行前判断是否有正在执行的操作
        SysUser sysUser = tokenService.getSysUser(ServletUtils.getRequest());
        examineeGradeInfoService.batchArchiveGrade(batchArchiveGradeVo,sysUser);
        return AjaxResult.success("归档成功");
    }

    @GetMapping("/getExamGrade/{examId}")
    public AjaxResult getExamGrade(@PathVariable String examId)
    {
        // 判断考试是否存在考生成绩信息
        boolean existExamineeFlag =  examineeGradeInfoService.seleExamineeCountById(examId);
        return AjaxResult.success(existExamineeFlag);
    }
}
